package com.chaosj.webmagic.convert;

import com.chaosj.webmagic.dto.gushiwen.HomeResponseDto;
import com.chaosj.webmagic.dto.SearchEnums;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Selectable;

/**
 * @className: HomeConvertHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
public class HomeDescConvertHandler extends AbstractConvertPage<HomeResponseDto> {

    @Override
    public void convert(Page page) {
        Selectable selectable = page.getHtml().xpath("//div[@class='right']/div[@class='sons']");
        HomeResponseDto hrd = createOrGet(page);
        selectable.nodes().forEach(selected->{
            // 获取 诗歌 分类
            String title = selected.xpath("//div[@class='title']/text()").get();
            selected.xpath("//div/a").nodes().forEach(node->{
                hrd.addObject("access", title, node.xpath("/a/text()").get(),node.xpath("/a/@href").get());
            });
        });
    }

    @Override
    public boolean support(SearchEnums instance) {
        return SearchEnums.HOME.equals(instance);
    }
}
